#pragma once
#include <iostream>
#include <odb/core.hxx>
#include <odb/nullable.hxx>
#include <boost/date_time/posix_time/posix_time.hpp>

namespace im{
    #pragma db object table("message")
    class Message{
    public:
        Message(){}
        Message(const std::string& message_id, const std::string& session_id, const std::string& user_id, unsigned char message_type, const boost::posix_time::ptime& create_time)
            :_message_id(message_id), _session_id(session_id), _user_id(user_id), _message_type(message_type), _create_time(create_time){}

        void message_id(const std::string& message_id){ _message_id = message_id; }
        std::string message_id() const { return _message_id; }

        void session_id(const std::string& session_id){ _session_id = session_id; }
        std::string session_id() const { return _session_id; }

        void user_id(const std::string& user_id){ _user_id = user_id; }
        std::string user_id() const { return _user_id; }

        void message_type(unsigned char message_type){ _message_type = message_type; }
        unsigned char message_type() const { return _message_type; }

        void create_time(const boost::posix_time::ptime& create_time){ _create_time = create_time; }
        boost::posix_time::ptime create_time() const { return _create_time; }

        void content(const std::string& content){ _content = content; }
        std::string content() const {
            if(_content)
                return *_content;
            return "";
        }

        
        void file_id(const std::string& file_id) { _file_id = file_id; }

        std::string file_id() const {
            if(_file_id)
                return *_file_id;
            return "";
        }
        void file_name(const std::string& file_name) { _file_name = file_name; }
        std::string file_name() const {
            if(_file_name)
                return *_file_name;
            return "";
        }

        void file_size(const unsigned int file_size) { _file_size = file_size; }
        unsigned int file_size() const {
            if(_file_size)
                return *_file_size;
            return 0;
        }

    private:
        friend class odb::access;
        #pragma db id auto
        unsigned long _id;
        #pragma db type("varchar(64)") index unique
        std::string _message_id;            //消息id
        #pragma db type("varchar(64)") index
        std::string _session_id;            //会话id
        #pragma db type("varchar(64)")
        std::string _user_id;               //发送者id
        unsigned char _message_type;
        #pragma db type("TIMESTAMP")
        boost::posix_time::ptime _create_time;     //消息的产生时间

        odb::nullable<std::string> _content;    //消息内容：只存储文本信息
        odb::nullable<std::string> _file_id;    //文件id
        odb::nullable<std::string> _file_name;  //文件名
        odb::nullable<unsigned int> _file_size;  //文件大小
    };

}
